package com.sjk.controller;
/*
 * @author 86136
 * @create 2023/5/15 22:33
 */

import com.sjk.pojo.Code;
import com.sjk.pojo.Result;
import com.sjk.pojo.User;
import com.sjk.pojo.Message;
import com.sjk.service.UserService;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {
    @Resource
    private UserService userService;

    // 密文显示
    @GetMapping("/users")
    public Result getAllUsers0(@Param("page") int page, @Param("pageSize") int pageSize) {
        // 处理分页   起始索引 = （查询页码 - 1）* 每页显示记录数
        int pageStart = (page - 1) * pageSize;
        List<User> userList = userService.getAll0(pageStart, pageSize);
        Integer code = userList != null ? Code.SUCCESS : Code.FAIL;
        String msg = userList != null ? Message.SUCCESS : Message.FAIL;
        return new Result(code, msg, userList);
    }

    // 明文显示
    @GetMapping("/sudousers")
    public Result getAllUsers1(@Param("page") int page,@Param("pageSize") int pageSize) {
        // 处理分页   起始索引 = （查询页码 - 1）* 每页显示记录数
        int pageStart = (page - 1) * pageSize;
        List<User> userList = userService.getAll1(pageStart, pageSize);
        Integer code = userList != null ? Code.SUCCESS : Code.FAIL;
        String msg = userList != null ? Message.SUCCESS : Message.FAIL;
        return new Result(code, msg, userList);
    }

    // 手机完整查询
    @GetMapping("/search-1")
    public Result getUserByAllPhonenum(String phonenum, int authority) {
        List<User> userList = userService.getByPhonenum(phonenum, authority);
        Integer code = userList != null ? Code.SUCCESS : Code.FAIL;
        String msg = userList != null ? Message.SUCCESS : Message.FAIL;
        return new Result(code, msg, userList);
    }

    // 手机后四位查询
    @GetMapping("/search-2")
    public Result getUserBy4Phonenum(String phonenum4, int authority) {
        List<User> userList = userService.getByPhonenum4(phonenum4, authority);
        Integer code = userList != null ? Code.SUCCESS : Code.FAIL;
        String msg = userList != null ? Message.SUCCESS : Message.FAIL;
        return new Result(code, msg, userList);
    }

    // 身份证号完整查询
    @GetMapping("/search-3")
    public Result getUserByAllId(String id, int authority) {
        List<User> userList = userService.getById(id, authority);
        Integer code = userList != null ? Code.SUCCESS : Code.FAIL;
        String msg = userList != null ? Message.SUCCESS : Message.FAIL;
        return new Result(code, msg, userList);
    }

    // 身份证号后六位查询
    @GetMapping("/search-4")
    public Result getUserBy6Id(String id6, int authority) {
        List<User> userList = userService.getById6(id6, authority);
        Integer code = userList != null ? Code.SUCCESS : Code.FAIL;
        String msg = userList != null ? Message.SUCCESS : Message.FAIL;
        return new Result(code, msg, userList);
    }

    // 删除前手机号查找
    @GetMapping("/delete/{phonenum}")
    public Result deleteGetUserByAllPhonenum(@PathVariable String phonenum) {
        List<User> userList = userService.deleteGetByPhonenum(phonenum);
        Integer code = userList != null ? Code.SUCCESS : Code.FAIL;
        String msg = userList != null ? Message.SUCCESS : Message.FAIL;
        return new Result(code, msg, userList);
    }

    // 删除用户
    @DeleteMapping("/delete/{phonenum}")
    public Result deleteUserByAllPhonenum(@PathVariable String phonenum) {
        userService.deleteByPhonenum(phonenum);
        return new Result(Code.SUCCESS, Message.SUCCESS, null);
    }

    // 更新前手机号查找
    @GetMapping("/update/{phonenum}")
    public Result updateGetUserByAllPhonenum(@PathVariable String phonenum) {
        List<User> userList = userService.updateGetByPhonenum(phonenum);
        Integer code = userList != null ? Code.SUCCESS : Code.FAIL;
        String msg = userList != null ? Message.SUCCESS : Message.FAIL;
        return new Result(code, msg, userList);
    }

    // 更新用户信息
    @PostMapping("/update")
    public Result updateUser(@RequestBody User user) {
        userService.updateUser(user);
        return new Result(Code.SUCCESS, Message.SUCCESS, null);
    }

    // 添加用户
    @PostMapping("/add")
    public Result addUser(@RequestBody User user) {
        userService.addUser(user);
        return new Result(Code.SUCCESS, Message.SUCCESS, null);
    }
}
